/**
 *  to login the user
 */
function login() {
    var user = document.getElementById("user").value;
    var password = document.getElementById("password").value;
    if (user == "" || password == "") {
        alert("请输入用户名密码");
    } else {
        $.ajax({
            url: 'control/login.php',
            type: 'post',
            data: {
                "user": user,
                "password": password
            },
            dataType: 'text',
            success: function (result) {
                if (result == "true") {
                    alert("登录成功");
                    window.location.href = "./index.html";
                } else {
                    alert("用户名或密码错误，登录失败");
                }
            }
        })
    }
}

function getUserName() {
    // 注：id选择器才支持innerHTML，class无效
    var userInfo = document.getElementById("user_name");
    var userLogout = document.getElementById("user_logout");
    $.ajax({
        url: 'control/getuser.php',
        dataType: 'text',
        success: function (result) {
            if (result) {
                userInfo.innerHTML = result;
                userLogout.innerHTML = "注销";
            } else {
                window.location.href = "./login.html";
            }
        }
    })
}
function userLogout() {
    $.ajax({
        url: 'control/logout.php',
        dataType: 'text',
        success: function (result) {
            if (result) {
                window.location.href = "./login.html";
            } else {
                console.log("注销失败");
            }
        }
    })
}
/**
 * 查询article中最大的id值
 */
function getCountArticle() {
    var value = "";
    $.ajax({
        url: 'control/countArticle.php',
        type: 'get',
        dataType: 'text',
        async: false,
        success: function (result) {
            value = result;
        }
    })
    return value;
}

/**
 * 获取文章标题列表
 */
function getTitles() {
    var listBox = document.getElementById("list_box");
    var count = getCountArticle();
    var html = '';

    for (var i = 1; i <= count; i++) {
        var jsonData;
        $.ajax({
            url: 'control/article.php',
            type: 'post',
            data: {
                "id": i
            },
            dataType: 'json',
            async: false,
            success: function (result) {
                jsonData = eval(result);
            }
        });
        if (JSON.stringify(jsonData) == "{}") {
            continue;
        } else {
            html += '<div>';
            html += '<a href="edioArticle.html?id=' + jsonData.id + '">';
            html += jsonData.title + '</a>';
            html += '<button style="float: right;margin-right: 10%" onclick="deleteArticle(' + jsonData.id + ')">删除</button>';
            html += '<p>' + jsonData.time + '</p>';
            html += '</div>';
            listBox.innerHTML = html;
        }
    }
}

function deleteArticle(id) {
    var status = confirm("确认删除该选项？");
    if (status) {
        $.ajax({
            url: 'control/deleteArticle.php',
            type: 'post',
            data: {
                "id": id
            },
            dataType: 'json',
            success: function (result) {
                if (result) {
                    alert("删除成功");
                    window.location.href = "index.html";
                }
            }
        })
    }
}